//
//  RefreshView.swift
//  微博
//
//  Created by admin on 2018/4/2.
//  Copyright © 2018年 xm. All rights reserved.
//

import UIKit

class RefreshView: UIView {

    
    /// 设置刷新状态
    var refreshState:RefreshState? {
        didSet{
            if refreshState == RefreshState.Normal {
                onNormalState()
            }else if refreshState == RefreshState.Pulling {
                onPullingState()
            }else if refreshState == RefreshState.Refreshing {
                onRefreshingState()
            }
        }
    }
    
    init() {
        super.init(frame: CGRect())
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        setupUI()
    }
    
    func onNormalState() {
        
    }
    
    func onPullingState() {
        
    }
    
    func onRefreshingState() {
        
    }
    
    
    //文字提示，懒加载，计算行属性 执行闭包
    fileprivate lazy var messageLable: UILabel = {
        let lab = UILabel()
        lab.font = UIFont.systemFont(ofSize: 14)
        lab.textColor = UIColor.darkGray
        lab.textAlignment = .center
        lab.text = "下拉刷新"
        return lab
    }()
    //下拉箭头
    fileprivate lazy var pullrefrshImageView: UIImageView = UIImageView(image: UIImage(named: "tableview_pull_refresh"))
    //小菊花
    fileprivate lazy var indicatorView: UIActivityIndicatorView = {
        let view = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.gray)
        return view
    }()
}

extension RefreshView {
    
    func setupUI() {
        
        // 添加控件
        addSubview(messageLable)
        addSubview(pullrefrshImageView)
        addSubview(indicatorView)
        //消息提示
        messageLable.translatesAutoresizingMaskIntoConstraints=false
        addConstraints([NSLayoutConstraint(item: messageLable, attribute: .centerX,
                                           relatedBy: .equal, toItem: self, attribute: .centerX,
                                           multiplier: 1.0, constant: pullrefrshImageView.bounds.width/2+2.5),
                        NSLayoutConstraint(item: messageLable, attribute: .centerY,
                                           relatedBy: .equal, toItem: self, attribute: .centerY,
                                           multiplier: 1.0, constant: 0)])
        //下拉箭头
        pullrefrshImageView.translatesAutoresizingMaskIntoConstraints=false
        addConstraints([NSLayoutConstraint(item: pullrefrshImageView, attribute: .right,
                                           relatedBy: .equal, toItem: messageLable, attribute: .left,
                                           multiplier: 1.0, constant: -5),
                        NSLayoutConstraint(item: pullrefrshImageView, attribute: .centerY,
                                           relatedBy: .equal, toItem: messageLable, attribute: .centerY,
                                           multiplier: 1.0, constant: 0)])
        //菊花
        indicatorView.translatesAutoresizingMaskIntoConstraints=false
        addConstraints([NSLayoutConstraint(item: indicatorView, attribute: .right,
                                           relatedBy: .equal, toItem: messageLable, attribute: .left,
                                           multiplier: 1.0, constant: -5),
                        NSLayoutConstraint(item: indicatorView, attribute: .centerY,
                                           relatedBy: .equal, toItem: messageLable, attribute: .centerY,
                                           multiplier: 1.0, constant: 0)])
    }
    
}
